class CacheStore {
  /**
   *
   * @param {string} type local/session
   */
  constructor(type = 'local') {
    this.cache = window[`${type}Storage`]
  }

  _has(key) {
    return this.cache.getItem(key) ? true : false
  }

  get(key, value = '') {
    value = typeof value === 'string' ? JSON.stringify(value) : value
    return this._has(key) ? JSON.parse(this.cache.getItem(key)) : value
  }
  set(key, value) {
    this.cache.setItem(key, JSON.stringify(value))
    return this
  }
  remove(key) {
    this._has(key) && this.cache.removeItem(key)
    return this
  }
}

export default (type = 'local') => new CacheStore(type)
